package com.qdhh.enrollment.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qdhh.enrollment.common.Result;
import com.qdhh.enrollment.model.QuizQuestion;
import com.qdhh.enrollment.service.AdminQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员题目管理控制器
 */
@Tag(name = "管理员题目管理", description = "题目的增删改查接口")
@RestController
@RequestMapping("/api/admin/questions")
public class AdminQuestionController {

    private final AdminQuestionService adminQuestionService;

    public AdminQuestionController(AdminQuestionService adminQuestionService) {
        this.adminQuestionService = adminQuestionService;
    }

    @Operation(summary = "分页查询题目列表")
    @GetMapping
    public Result<Page<QuizQuestion>> listQuestions(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size,
            @Parameter(description = "分类") @RequestParam(required = false) String category,
            @Parameter(description = "题型") @RequestParam(required = false) String type) {
        Page<QuizQuestion> result = adminQuestionService.listQuestions(page, size, category, type);
        return Result.success(result);
    }

    @Operation(summary = "根据ID获取题目")
    @GetMapping("/{id}")
    public Result<QuizQuestion> getQuestionById(@PathVariable String id) {
        QuizQuestion question = adminQuestionService.getQuestionById(id);
        return Result.success(question);
    }

    @Operation(summary = "创建题目")
    @PostMapping
    public Result<QuizQuestion> createQuestion(@RequestBody QuizQuestion question) {
        QuizQuestion result = adminQuestionService.createQuestion(question);
        return Result.success("创建成功", result);
    }

    @Operation(summary = "更新题目")
    @PutMapping("/{id}")
    public Result<QuizQuestion> updateQuestion(@PathVariable String id, @RequestBody QuizQuestion question) {
        QuizQuestion result = adminQuestionService.updateQuestion(id, question);
        return Result.success("更新成功", result);
    }

    @Operation(summary = "删除题目")
    @DeleteMapping("/{id}")
    public Result<Void> deleteQuestion(@PathVariable String id) {
        adminQuestionService.deleteQuestion(id);
        return Result.success("删除成功", null);
    }

    @Operation(summary = "批量删除题目")
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteQuestions(@RequestBody List<String> ids) {
        adminQuestionService.batchDeleteQuestions(ids);
        return Result.success("批量删除成功", null);
    }
}

